package study.javase.net.udp;





import java.net.DatagramPacket;
import java.net.DatagramSocket;


/**
 * 数据发送方 or 数据接收方  002
 * @author LiShaowen
 * @date 2020/9/5 21:23
 */
public class UdpTalker002 {
    public static void main(String[] args) throws Exception {
        System.out.println("talker002准备就绪");
        // 1.使用DatagramSocket 指定端口创建接收端
        DatagramSocket receiver = new DatagramSocket(9999);
        // 2.准备容器 封装成DatagramPacket
        byte[] container = new byte[1024*60];

        DatagramPacket packet = new DatagramPacket(container,0,container.length);
        while (true) {
            // 3.阻塞式接收
            receiver.receive(packet);
            // 4.分析数据
            byte[] datas = packet.getData();
            int length = packet.getLength();
            String data = new String(datas, 0, length);
            System.out.println(data);
            if ("bye".equals(data)) {
                break;
            }

        }
        // 5.释放资源
        receiver.close();
    }
}
